package com.nativetalk.common.ocs;

/**
 * Created by lizhun on 16/1/19.
 * <p>
 * <p>
 * Sample Code From www.aliyun.com
 */
/**

 *  Sample Code From www.aliyun.com

 */


import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.ConnectionFactoryBuilder.Protocol;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.PlainCallbackHandler;


public class OcsUtil {
    public static final String host = "03e5d595a0394a4a.m.cnhzaliqshpub001.ocs.aliyuncs.com";//控制台上的“内网地址”

    public static final String port = "11211"; //默认端口 11211，不用改

    public static final String username = "03e5d595a0394a4a";//控制台上的“访问账号“

    public static final String password = "Suerte2015";//邮件或短信中提供的“密码”

    /**
     * 向缓存添加新的键值对。如果键已经存在，则之前的值将被替换。
     *
     * @param key
     *            键
     * @param value
     *            值
     * @param expire
     *            过期时间 New Date(1000*10)：十秒后过期
     * @return
     */
    public static boolean add(String key, Object value, int expire) {
        boolean flag = false;
        try {
            AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
            MemcachedClient cache = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(host + ":" + port));
            cache.add(key, expire, value).get();

            flag = true;
        } catch (Exception e) {
            // 记录Memcached日志
            flag = false;
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 向缓存添加新的键值对。如果键已经存在，则之前的值将被替换。
     *
     * @param key
     *            键
     * @param value
     *            值
     * @param expire
     *            过期时间 New Date(1000*10)：十秒后过期
     * @return
     */
    public static boolean replace(String key, Object value, int expire) {
        boolean flag = false;
        try {
            AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
            MemcachedClient cache = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(host + ":" + port));
            cache.replace(key, expire, value);
            flag = true;
        } catch (Exception e) {
            // 记录Memcached日志

        }
        return flag;
    }

    /**
     * get 命令用于检索与之前添加的键值对相关的值。
     *
     * @param key
     *            键
     * @return
     */
    public static Object get(String key) {
        Object obj = null;
        try {
            AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
            MemcachedClient cache = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(host + ":" + port));
            obj = cache.get(key);
        } catch (Exception e) {

        }
        return obj;
    }

    /**
     * 删除 memcached 中的任何现有值。
     *
     * @param key
     *            键
     * @return
     */
    public static boolean delete(String key) {
        boolean flag = false;
        try {
            AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(username, password));
            MemcachedClient cache = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(host + ":" + port));
            cache.delete(key);
            flag = true;
        } catch (Exception e) {
            // 记录Memcached日志

        }
        return flag;
    }

}


